import 'package:cunyutech_flutter_ui/src/theme/main.dart';
import 'package:flutter/material.dart';

const DefaultCircleBorderGradient =
 LinearGradient(
  colors: [Color(0xFFF64949), Color(0xFF9F045C)],
  begin: FractionalOffset(0, 0),
  end: FractionalOffset(1, 1),
);

// final Color defaultShadowColor = CYColorsExtension.PrimaryColor.shade50.withOpacity(0.8);

class CircleBorderPointer extends CustomPainter {
  final double radius;
  final double borderWidth;
  final Color borderColor;
  final PaintingStyle borderStyle;
  final Gradient gradient;
  Color shadowColor;
  final double shadowWidth;

  CircleBorderPointer({ 
    this.borderWidth = 2, 
    this.borderColor = Colors.black, 
    this.borderStyle = PaintingStyle.stroke, 
    this.radius = 25,
    this.gradient = DefaultCircleBorderGradient,
    @required this.shadowColor,
    this.shadowWidth = 1, 
  });

  @override
  void paint(Canvas canvas, Size size) {
    final offsetCenter = Offset(radius, radius);
    final outerRadius = radius + shadowWidth;
    final innerRadius = radius - borderWidth - shadowWidth;
    final Rect arcRect = Rect.fromCircle(center: offsetCenter, radius: radius);
    final borderPaint = Paint()
      ..color = borderColor
      ..style = borderStyle
      ..strokeWidth = borderWidth
      ..shader = gradient.createShader(arcRect);

    canvas.drawCircle(offsetCenter, radius - borderWidth / 2, borderPaint);

    final shadowPaint = Paint()
          ..style = PaintingStyle.stroke
          ..color = shadowColor
          ..strokeWidth = shadowWidth
          ..maskFilter = MaskFilter.blur(BlurStyle.normal, shadowWidth);
    
    canvas.drawCircle(offsetCenter, innerRadius, shadowPaint);
    canvas.drawCircle(offsetCenter, outerRadius, shadowPaint);
  }
  
  @override
  bool shouldRepaint(CustomPainter oldDelegate) {
    return true;
  }
}

class CYCircleBorder extends StatelessWidget {
  final Widget child;
  final double radius;
  final EdgeInsetsGeometry margin;

  const CYCircleBorder({Key key, this.child, this.radius = 27, this.margin}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    var cs = CYThemeData.of(context);
    return 
    Container(
      margin: margin,
      child: Stack(
          alignment: AlignmentDirectional.center,
          children: <Widget>[
            Container(
              width: radius*2,
              height: radius*2,
              child: CustomPaint(
                size: Size(radius, radius),
                painter: CircleBorderPointer(
                  radius: radius, 
                  borderWidth: 2,
                  shadowColor: cs.linkColor.withOpacity(0.5)
                ),
              ),
            ),
            child,
          ],
      )
    );
  }
}